﻿using HomeCareLibrary.Models;
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Data;
using System.Net;
using System.Configuration;
using System.Text;
using System.IO;

namespace HomeCareLibrary
{
    public class DataRepository
    {

        private homewizard_test2databaseEntities db;

        public DataRepository()
        {
            db = new homewizard_test2databaseEntities();
        }


        #region[Personalization Mobile]

        public UserDetailIDModel getDwellingtype_UserDetail(Int32 userID, Int32 homeNumber)
        {
            var model = new UserDetailIDModel();
            model = (from e in db.User_Details
                     where e.user_id == userID && e.home_number == homeNumber
                     orderby e.home_number
                     select new UserDetailIDModel()
                     {
                         user_id = e.user_id,
                         home_number = e.home_number,
                         home_name = e.home_name,
                         dwelling_type = e.dwelling_type,
                     }).SingleOrDefault();


            return model;
        }


        public Int32 getMultipleHomesByUserID(Int32 userID)
        {
           
           var getHomeNumber = (from e in db.User_Details
                     where e.user_id == userID
                     orderby e.home_number
                     select e.home_number).FirstOrDefault();


           return getHomeNumber;
        }

        public UserDetailIDModel getGoals_UserDetail(Int32 userID, Int32 homeNumber)
        {
            var model = new UserDetailIDModel();

            model = (from e in db.User_Details
                     where e.user_id == userID && e.home_number == homeNumber
                     orderby e.home_number
                     select new UserDetailIDModel()
                     {
                         user_id = e.user_id,
                         home_number = e.home_number,
                         ps_goal_string = e.ps_goal_string
                     }).SingleOrDefault();

            return model;
        }
        public UserDetailIDModel getHomeFeatures_UserDetail(Int32 userID, Int32 homeNumber)
        {
            var model = new UserDetailIDModel();
            model = (from e in db.User_Details
                     where e.user_id == userID && e.home_number == homeNumber
                     orderby e.home_number
                     select new UserDetailIDModel()
                     {
                         user_id = e.user_id,
                         home_number = e.home_number,
                         featurestring = e.featurestring,
                         dwelling_type = e.dwelling_type
                     }).SingleOrDefault();
            return model;
        }
        public UserDetailIDModel getLocalClimate_UserDetail(Int32 userID, Int32 homeNumber)
        {
            var model = new UserDetailIDModel();
            model = (from e in db.User_Details
                     where e.user_id == userID && e.home_number == homeNumber
                     orderby e.home_number
                     select new UserDetailIDModel()
                     {
                         user_id = e.user_id,
                         home_number = e.home_number,
                         ps_country = e.ps_country,
                         ps_state_province = e.ps_state_province
                     }).SingleOrDefault();
            return model;
        }
        public UserDetailIDModel getKnowledgeLevel_UserDetail(Int32 userID, Int32 homeNumber)
        {
            var model = new UserDetailIDModel();

            model = (from e in db.User_Details
                     where e.user_id == userID && e.home_number == homeNumber
                     orderby e.home_number
                     select new UserDetailIDModel()
                     {
                         user_id = e.user_id,
                         home_number = e.home_number,
                         ps_knowledge_level = e.ps_knowledge_level
                     }).SingleOrDefault();

            return model;
        }
        public UserDetailIDModel getPriority_UserDetail(Int32 userID, Int32 homeNumber)
        {
            var model = new UserDetailIDModel();
           
            model = (from e in db.User_Details
                     where e.user_id == userID && e.home_number == homeNumber
                     orderby e.home_number
                     select new UserDetailIDModel()
                     {
                         user_id = e.user_id,
                         home_number = e.home_number,
                         ps_priority = e.ps_priority
                     }).SingleOrDefault();
            return model;

        }
        public UserDetailIDModel getMonthlyAlert_UserDetail(Int32 userID, Int32 homeNumber)
        {
            var model = new UserDetailIDModel();

            model = (from e in db.User_Details
                     join UID in db.User_ID on new { User_ID = e.user_id } equals new { User_ID = UID.user_id1 }
                     where e.user_id == userID && e.home_number == homeNumber
                     orderby e.home_number
                     select new UserDetailIDModel()
                     {
                         user_id = e.user_id,
                         home_number = e.home_number,
                         web_mo_alert_email = UID.web_mo_alert_email,
                         app_mo_alert_week = UID.app_mo_alert_week,
                         app_mo_alert_day = UID.app_mo_alert_day,
                         app_mo_alert_hour = UID.app_mo_alert_hour,
                         app_mo_alert_badge = UID.app_mo_alert_badge,
                         app_mo_alert_popup = UID.app_mo_alert_popup

                     }).SingleOrDefault();
            return model;
        }

        public UserDetailIDModel getAdminMonthlyAlert_UserDetail(Int32 userID)
        {
            var model = new UserDetailIDModel();

            model = (from e in db.User_ID
                     where e.user_id1 == userID 
                     select new UserDetailIDModel()
                     {
                         user_id = e.user_id1,
                         web_mo_alert_email = e.web_mo_alert_email,
                         app_mo_alert_week = e.app_mo_alert_week,
                         app_mo_alert_day = e.app_mo_alert_day,
                         app_mo_alert_hour = e.app_mo_alert_hour,
                         app_mo_alert_badge = e.app_mo_alert_badge,
                         app_mo_alert_popup = e.app_mo_alert_popup

                     }).SingleOrDefault();
            return model;
        }

        public PromoCodeModel getAdminPromoCode_UserID(Int32 userID)
        {
            var model = new PromoCodeModel();

            model = (from e in db.User_ID
                     where e.user_id1 == userID
                     select new PromoCodeModel()
                     {
                         user_id = e.user_id1,
                         sponsor=e.sponsor
                     }).SingleOrDefault();
            return model;
        }

        public IEnumerable<sp_Update_UsernamePassword_Result> callsp_update(int id, string username, string password)
        {
            var result = db.sp_Update_UsernamePassword(id, username, password);
            return result.ToList();
        }
        public IEnumerable<sp_Web_Initial_Setup_Result> Web_Initial_Setup_Result(string paramUserName, string paramPassword, string paramPromoCode)
        {
            var result = db.sp_Web_Initial_Setup(paramUserName, paramPassword, paramPromoCode);
            return result.ToList();
        }
        public IEnumerable<sp_Web_Login_Result> login_Result(string paramUserName, string paramPassword)
        {
            var result = db.sp_Web_Login(paramUserName, paramPassword);
            return result.ToList();
        
        }

        public LoginPasswordModel getLogin_UserID(Int32 userID)
        {
            var model = new LoginPasswordModel();

            model = (from e in db.User_ID
                     where e.user_id1 == userID
                     select new LoginPasswordModel()
                     {
                         user_id = e.user_id1,
                         user_name = e.user_name,
                         password=e.password
                     }).SingleOrDefault();
            return model;
        }

        public IEnumerable<sp_Update_Promocode_Result> updatePromocode(Int32? userid, String Promocode, String DeletePromocode, String ChangePromocode)
        {
            var result=db.sp_Update_Promocode(userid,Promocode,DeletePromocode,ChangePromocode);
            return result.ToList();
            
        }


       
        public  IEnumerable<Dwelling_TypesModel> BindDwellingType()
        {

            return (from p in db.Dwelling_Types
                                        select new Dwelling_TypesModel()
                                        {
                                            dwelling_type = p.dwelling_type,
                                            dwelling_code = p.dwelling_code
                                        }).ToList();

             
        }
        public  IEnumerable<HomeCareLibraryCategoryModel> BindFeaturesCategory()
        {
          return  (from p in db.HomeCareLibraryCategories   //We are not taking the goals category.
             where p.ID != 7
             select new HomeCareLibraryCategoryModel()
             {
                 ID = p.ID,
                 HomeCare_Category = p.HomeCareLibrary_Category
             }).ToList();
        }
        public IEnumerable<HomeCareModel> BindFeaturesSubCategory(Int32? dwelling_type)
        {
          return  (from DF in db.Dwelling_Features
             join F in db.Features on DF.feature_code equals F.feature_code
             where
               DF.dwelling_code == dwelling_type
             orderby DF.ID
             select new HomeCareModel()
             {
                 CategoryID = DF.categoryID,
                 Feature_code = DF.feature_code.Trim(),
                 Feature_Name = F.feature_name,
             }).ToList();
        }
        public SubmenuModel getSubmenuModel(string id)
        {
            var model = new SubmenuModel();
            model = (from p in db.Features
                     where p.feature_code == id
                     select new SubmenuModel()
                     {
                         CategoryID = p.categoryID,
                         Feature_code = p.feature_code.Trim(),
                         Feature_Name = p.feature_name,
                         Feature_description = p.feature_description,
                         feature_url = p.feature_url
                     }).FirstOrDefault();
            return model;
        }

        #endregion

        #region [Personalization Desktop]
          public UserDetailIDModel getPersonalization_UserDetail(Int32 userID, Int32 homeNumber)
        {
            var model = new UserDetailIDModel();

            model = (from e in db.User_Details
                     join UID in db.User_ID on new { User_ID = e.user_id } equals new { User_ID = UID.user_id1 }
                     where e.user_id == userID && e.home_number == homeNumber
                     orderby e.home_number
                     select new UserDetailIDModel()
                     {
                         user_id = e.user_id,
                         home_number = e.home_number,
                         home_name = e.home_name,
                         dwelling_type = e.dwelling_type,
                         featurestring = e.featurestring,
                         ps_priority = e.ps_priority,
                         ps_knowledge_level = e.ps_knowledge_level,
                         ps_country = e.ps_country,
                         ps_state_province = e.ps_state_province,
                         ps_goal_string = e.ps_goal_string,
                         turn_offs = e.turn_offs,
                         start_month = e.start_month,
                         start_year = e.start_year,
                         last_crc_number = e.last_crc_number,
                         welcome_image = UID.welcome_image,
                         welcome_image_chg_time = UID.welcome_image_chg_time,
                         web_mo_alert_email = UID.web_mo_alert_email,
                         app_mo_alert_week = UID.app_mo_alert_week,
                         app_mo_alert_day = UID.app_mo_alert_day,
                         app_mo_alert_hour = UID.app_mo_alert_hour,
                         app_mo_alert_badge = UID.app_mo_alert_badge,
                         app_mo_alert_popup = UID.app_mo_alert_popup

                     }).SingleOrDefault();
            return model;
        }

          public UserDetailIDModel GetPersonalizationDefault()
          {
              var model = new UserDetailIDModel();

              model = (from e in db.User_Details
                       join UID in db.User_ID on new { User_ID = e.user_id } equals new { User_ID = UID.user_id1 }
                       where e.user_id == 1
                       orderby e.home_number
                       select new UserDetailIDModel()
                       {
                           user_id = e.user_id,
                           home_number = e.home_number,
                           home_name = e.home_name,
                           dwelling_type = e.dwelling_type,
                           featurestring = e.featurestring,
                           ps_priority = e.ps_priority,
                           ps_knowledge_level = e.ps_knowledge_level,
                           ps_country = e.ps_country,
                           ps_state_province = e.ps_state_province,
                           ps_goal_string = e.ps_goal_string,
                           turn_offs = e.turn_offs,
                           start_month = e.start_month,
                           start_year = e.start_year,
                           last_crc_number = e.last_crc_number,
                           welcome_image = UID.welcome_image,
                           welcome_image_chg_time = UID.welcome_image_chg_time,
                           web_mo_alert_email = UID.web_mo_alert_email,
                           app_mo_alert_week = UID.app_mo_alert_week,
                           app_mo_alert_day = UID.app_mo_alert_day,
                           app_mo_alert_hour = UID.app_mo_alert_hour,
                           app_mo_alert_badge = UID.app_mo_alert_badge,
                           app_mo_alert_popup = UID.app_mo_alert_popup

                       }).SingleOrDefault();
              return model;
          }
         

        #endregion

        #region[Tasks, tips, ideas]

          public string getTaskTipsIdeasWebService(String dwell_priority_kl_loc, String feature_string,String ps_goal_string, String turn_offs, String startmonth, String startyear, String requestedmonth, String requestedyear,String WebServiceName,int user_id,int home_number)
          {
              //HomeCareLibrary.AppService.
             Core.DataContracts.GetRoutineTasksRequest obj = new Core.DataContracts.GetRoutineTasksRequest();
             obj.DwellPriorityKLLoc = dwell_priority_kl_loc;
             obj.FeatureString = feature_string;
             obj.Goal = ps_goal_string;
             obj.Turn_Offs = turn_offs;
             obj.StartMonth = Convert.ToInt32(startmonth);
             obj.StartYear = Convert.ToInt32(startyear);
             obj.RequestedMonth = Convert.ToInt32(requestedmonth);
             obj.RequestedYear = Convert.ToInt32(requestedyear);
             obj.UserId = user_id;
             obj.HomeNumber = home_number;
            
            
             AppService.TasksAppService tasks = new AppService.TasksAppService();
             var str=tasks.GetRoutineTasks(obj);

              //string postData;
              //HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(Convert.ToString(ConfigurationManager.AppSettings["PostUrl"]) + WebServiceName);

           
              //postData = "dwell_priority_kl_loc=" + dwell_priority_kl_loc + "/feature_string=" + feature_string + "/ps_goal_string=" + ps_goal_string + "/turn_offs=" + turn_offs + "/startmonth=" + startmonth + "/startyear=" + startyear + "/requestedmonth=" + requestedmonth + "/requestedyear=" + requestedyear;
              //ASCIIEncoding encoding = new ASCIIEncoding();
              //byte[] data = encoding.GetBytes(postData);

              //httpWReq.Method = "POST";
              //httpWReq.ContentType = "application/x-www-form-urlencoded";
              //httpWReq.ContentLength = data.Length;

              //using (Stream stream = httpWReq.GetRequestStream())
              //{
              //    stream.Write(data, 0, data.Length);
              //}

              //HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
              //string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

             return str.ToString();
              
          }
        
        #endregion



        #region [Admin]

          public void UpdateWebMonthlyReminder(UserDetailIDModel model)
          {
              var userIDS = (from u in db.User_ID
                             where u.user_id1 == model.user_id
                             select u).First();
              userIDS.web_mo_alert_email = model.web_mo_alert_email;

                  db.User_ID.ApplyCurrentValues(userIDS);
                  db.SaveChanges();
             
          }


          public void UpdateAndroidMonthlyReminder(UserDetailIDModel model)
          {
              var userIDS = (from u in db.User_ID
                             where u.user_id1 == model.user_id
                             select u).First();
              userIDS.web_mo_alert_email = model.web_mo_alert_email;
              userIDS.app_mo_alert_badge = model.app_mo_alert_badge;
              userIDS.app_mo_alert_popup = model.app_mo_alert_popup;
              userIDS.app_mo_alert_week = model.app_mo_alert_week;
              userIDS.app_mo_alert_day = model.app_mo_alert_day;
              userIDS.app_mo_alert_hour = model.app_mo_alert_hour;

              db.User_ID.ApplyCurrentValues(userIDS);
              db.SaveChanges();
          }
        #endregion
    }
}